//
//  ChooseNumView.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/14.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "ChooseNumView.h"
#import "ChooseNumTableViewCell.h"
@interface ChooseNumView()<UITableViewDelegate,UITableViewDataSource>

Copy void(^selectedHandle)(NSString *str);
Strong UITableView *tableView;

//底部视图
Strong UIButton * bottomBtn;

//头部视图
Strong UIView * headerView;
Strong UILabel * countLabel;

//尾部视图
Strong UIView * footView;
Strong UILabel * pihaoLabel;
Strong UILabel * pihaoDetalLabel;

@end

@implementation ChooseNumView

#pragma mark -- Action

- (void)bottomBtnClick{
    
    
    
}


#pragma init --初始化
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.tableView];
        [self addSubview:self.bottomBtn];
        
    }
    return self;
}

#pragma mark -- 显示还是不显示
//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UNIT_HEIGHT(200), SCREEN_WIDTH, SCREEN_HEIGHT - UNIT_HEIGHT(49)- UNIT_HEIGHT(200)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.bounces = NO;
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(35))];
        
        _countLabel = [[UILabel alloc] init];
        //_countLabel.text = @" 购买数量:10";
        _countLabel.text = [NSString stringWithFormat:@"购买数量: %ld",(long)self.count];
        _countLabel.font = DEF_FontSize_14;
        _countLabel.textColor = getColor(@"323232");
        
        [_headerView addSubview:_countLabel];
        [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(UNIT_WIDTH(-15));
        }];
        
    }
    return _headerView;
}

-(UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(35))];
        
        _pihaoLabel = [[UILabel alloc] init];
        _pihaoLabel.textColor = getColor(@"666666");
        _pihaoLabel.font = DEF_FontSize_14;
        _pihaoLabel.text = @"批号-批次";
        
        _pihaoDetalLabel = [[UILabel alloc] init];
        _pihaoDetalLabel.textColor = getColor(@"323232");
        _pihaoDetalLabel.font = DEF_FontSize_14;
        _pihaoDetalLabel.text = @"R31916-170329026";
        
        [_footView sd_addSubviews:@[_pihaoLabel,_pihaoDetalLabel]];
        
        [_pihaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(UNIT_WIDTH(50));
        }];
        
        [_pihaoDetalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(UNIT_WIDTH(-16));
        }];
        
    }
    return _footView;
}

// 底部视图

- (UIButton *)bottomBtn{
    if (!_bottomBtn) {
        _bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tableView.frame), SCREEN_WIDTH, UNIT_HEIGHT(49))];
        _bottomBtn.backgroundColor = [UIColor whiteColor];
        [_bottomBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_bottomBtn setTitleColor:getColor(@"323232") forState:UIControlStateNormal];
        [_bottomBtn addTarget:self action:@selector(bottomBtnClick) forControlEvents:UIControlEventTouchUpInside];

    }
    return _bottomBtn;
}
 
#pragma mark - <UITableViewDelegate , UITableViewDataSource>
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 1;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        
        return self.chooseArr.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        ChooseNumTableViewCell * cell = [ChooseNumTableViewCell cellForTableView:tableView];
        
        cell.model = self.chooseArr[indexPath.row];
        
        cell.getPlusClick = ^(ChooseNumTableViewCell *cell) {
            
            [UIView animateWithDuration:1 animations:^{
                cell.textField.hidden = NO;
                cell.minusBtn.hidden = NO;
                NSInteger count = [cell.textField.text integerValue];
                count += 1;
                
                NSString *countString = [NSString stringWithFormat:@"%ld",count];
                cell.textField.text = countString;
            }];
        };
        
        cell.getMiuteClick = ^(ChooseNumTableViewCell *cell) {
            
            NSInteger count = [cell.textField.text integerValue];
            count -= 1;
            
            NSString *countString = [NSString stringWithFormat:@"%ld",count];
            cell.textField.text = countString;
            
           if([cell.textField.text isEqualToString:@"0"]){
                cell.textField.hidden = YES;
                cell.minusBtn.hidden = YES;
            }
        };
        
        cell.getTextClik = ^(ChooseNumTableViewCell *cell) {
            
            [UIView animateWithDuration:1 animations:^{
                
                self.count = cell.countSpph;
                
                cell.textField.text = [NSString stringWithFormat:@"%ld", (long)self.count];
                
                if([cell.textField.text isEqualToString:@"0"]){
                    cell.textField.hidden = YES;
                    cell.minusBtn.hidden = YES;
                    
                }
            }];
        
        };
        
        return cell;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return UNIT_HEIGHT(5);
    }
    

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return UNIT_HEIGHT(200);
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        
    }
    


@end
